package com.ghh;

import java.io.File;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import junit.framework.TestCase;

public class CodeTest extends TestCase {
	public void testUUID() {
		UUID uuid = UUID.randomUUID();
		System.out.println(uuid.toString());
		System.out.println(uuid.getLeastSignificantBits());
		System.out.println(uuid.getMostSignificantBits());
	}

	public void testJAXB() throws JAXBException {
		JAXBContext ctx = JAXBContext.newInstance(User.class);
		Marshaller ms = ctx.createMarshaller();
		StringWriter s = new StringWriter();
		User u = new User();
		u.setId(100l);
		u.setName("ghh");

		System.out.println(u.getClass());

		List<String> ul = new ArrayList<String>();
		ul.add("a");
		ul.add("b");
		ul.add("c");

		u.setUsers(ul);
		ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		ms.marshal(u, s);
		System.out.println(s.toString());
	}

	public void testPath() {
		File f = new File("a");
		System.out.println(f.getAbsolutePath());
	}

	public void testParseURL() throws URISyntaxException {
		URI url = new URI("sftp://seangu:password@com.ghh:80/res/a/ee.txt");
		System.out.println(url.getScheme());
		System.out.println(url.getHost());
		System.out.println(url.getPort());
		System.out.println(url.getPath());
		System.out.println(url.getUserInfo());
	}

	public void testTempDir() {
		System.out.println(System.getProperty("java.io.tmpdir"));
	}

	@SuppressWarnings("unused")
	public void testAAA() {
		String s = null;
		System.out.println(s = "sb");
	}

	public void testStringFormat() throws UnknownHostException {
		System.out.println(String.format("%1$tY/%1$tm/%1$td - %2$tY/%2$tm/%2$td - %1$tF", new Date(), new Date()));
		System.out.println(String.format("%1$3.2f, %2$4d", 3.1415926, 1234));
		System.out.println(String.format("%1$c", '\u534e'));
		System.out.println(String.format("%1$-5d,", 15));
		System.out.println(String.format("icon_%1$07d", 11));
	}

	public void testTimer() throws Exception {
		final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
		final Timer timer = new Timer(false);
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				System.out.println("1 #" + Thread.currentThread().getId() + " current time is:" + sdf.format(new Date()));
			}
		}, sdf.parse("20120412 13:59:50"), 5000);

		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				System.out.println("2 #" + Thread.currentThread().getId() + " current time is:" + sdf.format(new Date()));
			}
		}, 5000, 5000);

		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				timer.cancel();
				System.out.println("cancel all");
			}
		}, 30000);

		// Thread.sleep(1000000);
	}
	
	public void testDateFormat() throws Exception {
		Calendar c = Calendar.getInstance();
		c.set(2012, 5, 11, 0, 0, 0);
		System.out.println(String.format(Locale.ENGLISH, "%1$tF %1$tr(%1$tT)", c.getTime()));
		
		SimpleDateFormat sf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a", Locale.ENGLISH);
		System.out.println(sf.format(c.getTime()));
		System.out.println(c.getTime());
		System.out.println(String.format("%1$tF %1$tr(%1$tT)", sf.parse(sf.format(c.getTime()))));
		
		//System.out.println(String.format("%1$tF %1$tr(%1$tT)", sf.parse("06/05/2012 12:00:00 AM")));
	}
	
	public void testDateFormat2() throws ParseException {
		String sd = "06/12/2012 13:00 PM";
		SimpleDateFormat sf = new SimpleDateFormat("MM/dd/yyyy h:mm a");
		Date d = sf.parse(sd);
		System.out.println(sf.format(d) + "-->" + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(d));
	}
	
	public void testGetUnicode() {
		char c = 'Ã';
		System.out.println(Integer.toHexString(c));
	}
	
	public void testCharset() {
		System.out.println(Charset.defaultCharset());
	}
	
	public void testRegxPattern() {
		String fileName = "white.png";
		Pattern reg=Pattern.compile("[.](jpg|png|jpeg|gif)$", Pattern.CASE_INSENSITIVE);
		Matcher matcher=reg.matcher(fileName);
		if(matcher.find()) {
			System.out.println(matcher.group(0));
		}
	}
	
	public void testSplit() {
		System.out.println(Arrays.toString("苏州   ++ ".split("[\\s+]+")));
		System.out.println(String.format("%1$s like :%2$s", "e.name", "keyword1"));
	}
	
	public void testBoolean() {
		Boolean b = Boolean.TRUE;
		if (b) {
			System.out.println(true);
		} else {
			System.out.println(false);
		}
	}
	
	public void testDateLocale() {
		Calendar c = Calendar.getInstance(Locale.CHINESE);
		System.out.println(c.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.CHINESE));
		System.out.println(String.format(Locale.CHINESE, "%1$tm月%1$te日 %1$tA", c));
	}
	
	public void testRegx() {
		Pattern p = Pattern.compile("^(.+?)/(.+)$");
		Matcher m = p.matcher("Person/p/ser.dat");
		if (m.matches()) {
			System.out.println(m.group(1));
		} else {
			System.out.println("not match");
		}
	}
	
	public void testRegx2() {
		Matcher m = Pattern.compile("^/*(.*?)\\$*/*\\$*$").matcher("hcm/dataloader/import");
		if (m.find()) {
			System.out.println(m.groupCount());
			System.out.println(m.group(1));
		}
		System.out.println(m.replaceFirst("$1\\$"));
		System.out.println("aabbccddeeff".replaceAll("(([a-z])\\2)", "$1x"));
	}

	public void testRegx3() {
		String s = "hcm/dataloader$/import";
		String s2 = s.replaceAll("([^/$]+(?=[/])|$)", "$1\\$");
		System.out.println(s2);
	}
	
	public void testMath() {
		System.out.println(Math.pow(2, 3));
		System.out.println(Math.pow(32, 0.2d));
	}
	
	public void testUnicodeChar() throws UnsupportedEncodingException {
		String s = "中";
		for (byte b : s.getBytes()) {
			System.out.print(Integer.toString(b & 0xFF, 16) + " ");
		}
		System.out.println();
		String ns = new String(s.getBytes(), "gbk");
		System.out.println(ns);
		for (byte b : ns.getBytes()) {
			System.out.print(Integer.toString(b & 0xFF, 16) + " ");
		}
	}
	
	public void testStringCat() {
		String[] sarr = {"a", "b", "c"};
		System.out.println(Arrays.toString(sarr));
	}
	
	public void testStringCompare() {
		System.out.println("2.2".compareTo("2.1.1121545"));
		System.out.println(new Timestamp(System.currentTimeMillis()));
	}
	
	public static void main(String[] args) {
		System.out.println(Charset.defaultCharset());
	}
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class User {
	@XmlAttribute
	private Long	     id;
	@XmlElement(name = "userName")
	private String	     name;
	@XmlElement(name = "Users")
	private List<String>	users;

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<String> getUsers() {
		return users;
	}
	public void setUsers(List<String> users) {
		this.users = users;
	}
}
